<?php

namespace App\Http\Controllers\Admin;

use GuzzleHttp\Cookie\CookieJar;
use Illuminate\Http\Request;
use Illuminate\Cookie;



class MenuController extends BaseController
{
    protected $request;
    protected $api_url = [
        'index' => ['get', 'menu'],
        'show' => ['get', 'menu/%d'],
        'store' => ['post', 'menu'],
        'update' => ['put', 'menu/%d'],
        'destroy' => ['delete', 'menu/%d'],
        
        'checkRoute' => ['get', 'menu/checkRoute'],
    ];
    public function __construct(Request $request)
    {
        $this->request=$request;
    }

    public function index(Request $request)
    {	
        $res = http_admin($this->api_url['index'])->send(['page' => $request->input('page',1)]);
        
        $data = paginate($request, $res['data']);
        return view('Admin.menu.index',$data);
    }
    
    public function edit($id){
        $res = http_admin($this->api_url['show'], [$id])->send();

        return view('Admin.menu.edit',$res['data']);
    }


    public function create(){
        return view('Admin.menu.create');
    }

    public function store(Request $request)
    {
        $this->form();
        $data = $request->except('_token','_method');
        $menu = http_admin($this->api_url['store'])->send($data);
        return redirect('/foradmin/menu');
    }

    public function update($id,Request $request)
    {
        $this->form();
        $data = $request->except('_token','_method');
        $res = http_admin($this->api_url['update'], [$id])->send($data);
        return redirect('/foradmin/menu');
        
    }

    public function destroy($id)
    {
        $res = http_admin($this->api_url['destroy'], [$id])->send();
        return redirect('/foradmin/menu');
    }
    
    public function form(){
        $this->validate($this->request,[
            'name'  => 'required',
            'route' => 'required',
            'apiroute' => 'required',
            'description' => 'required',
        ], [
            'name.required' => '菜单名称必填',
            'route.required' => '菜单路由必填',
            'apiroute.required' => '接口路径必填',
            'description.required' => '描述信息必填',
        ]);

        return $this->request->all();
    }
    /**
     * checkRoute 
     * 检测输入的路由是否可存入 menu_permission 表
     * 即判断是否已添加过该菜单路由
     * */
    public function checkRoute(){
        $data = $this->request->all(['route']);

        if (!isset($data['route']) || $data['route'] == null) {
            return response()->json(['code' => 404, 'message' => '请输入正确的路由']);
        }
        $res = http_admin($this->api_url['checkRoute'])->send($data);

        try {
            if (isset($res['code']) && $res['code'] == 0) {
                return response()->json(['code' => 0,'message'=>'验证通过']);
            } else {
                return response()->json(['code' => 404, 'message' => '请输入正确的后台路由']);
            }
        }catch (\Exception $e){
            dd($e->getMessage());
            return response()->json(['code' => 404,'message'=>$e->getMessage()]);
        }
    }
}